// Original Author: Marc Belmont <code at marcbelmont.com>
// Contributors: Alex Doll <alex.doll at agdconsulting.ca>


////////////////////////
// Constructor / init //
////////////////////////

function Conversions() {
  this.init();
}


Conversions.prototype = {

  init : function(calc) {
  },

  //////////////
  // commands //
  //////////////

  selectFunction : function(func, params) {
    var res;
    if (func == "base2to") {
      res = this.ToBase10("2", params[1]);
      res = this.base10To(params[0], res);
    } else if (func == "base8to") {
      res = this.ToBase10("8", params[1]);
      res = this.base10To(params[0], res);
    } else if (func == "base10to") {
      res = this.base10To(params[0], params[1]);
    } else if (func == "base16to") {
      res = this.ToBase10("16", params[1]);
      res = this.base10To(params[0], res);

      // Simple convertions
    } else {
      res = this.simpleConv(func, params[0]);
    }
    return res;
  },

  //////////////////////////
  // Conversion functions //
  //////////////////////////

  simpleConv : function(func, num) {
    var conv = new Array(
			 // Length
			 ["fathomtoft","6"],
			 ["fathomtom","1.8293"],

 			 ["inchtofeet","0.08333333333333"],
 			 ["inchtoyard","0.02777777777778"],
 			 ["inchtomile","0.00001578282828"],
 			 ["inchtometer","0.0254"],

 			 ["feettoinch","12"],
 			 ["feettoyard","0.33333333333333"],
 			 ["feettomile","0.00018939393939"],
 			 ["feettometer","0.3048"],

 			 ["yardtoinch","36"],
 			 ["yardtofeet","3"],
 			 ["yardtomile","0.00056818181818"],
 			 ["yardtometer","0.9144"],

 			 ["miletoinch","63360"],
 			 ["miletofeet","5280"],
 			 ["miletoyard","1760"],
 			 ["miletometer","1609.344"],

 			 ["metertoinch","39.3700787401574803149606299212"],
 			 ["metertofeet","3.2808398950131233595800524934383"],
 			 ["metertoyard","1.0936132983377077865266841644794"],
 			 ["metertomile","0.00062137119223733396961743418436332"],

			 // Area TODO
			 ["acrestosqft","43560"],
			 ["sqfttoacres","0.00002296"],
			 ["acrestom2","4047"],
			 ["acrestoha","0.4047"],
			 ["m2toacres","0.0002471"],
			 ["sqmilestoacres","640"],
			 ["acrestosqmiles","0.0015625"],
			 ["hatoactres","2.471"],
			 ["hatosqft","107600"],
			 ["sqfttom2","0.0929"],
			 ["km2toacres","247.1"],
			 ["km2tosqft","1.08e7"],
			 ["km2tosqmiles","0.3861"],
			 ["m2tosqft","10.76"],
			 ["sqmilestokm2","2.59"],

			 // Mass
 			 ["poundstoounce","16.000002821917267029565227206669"],
 			 ["poundstograms","453.5924"],
 			 ["poundstokilograms","0.4535924"],

 			 ["ouncetopounds","0.062499988976887619810208460282844"],
 			 ["ouncetograms","28.34952"],
 			 ["ouncetokilograms","0.02834952"],

 			 ["gramstopounds","0.0022046224760379583079434311509628"],
 			 ["gramstoounce","0.035273965837869565340083359436068"],
 			 ["gramstokilograms","0.001"],

 			 ["kilogramstopounds","2.2046224760379583079434311509628"],
 			 ["kilogramstoounce","35.273965837869565340083359436068"],
 			 ["kilogramstograms","1000"],

			 // Mass TODO
			 ["assaysttogram","29.17"],
			 ["gtooz_t","0.03215"],
			 ["kgtolt","0.000984"],
			 ["kgtost","0.0011023"],
			 ["oz_avtooz_t","0.9115"],
			 ["oz_ttog","31.1"],
			 ["oz_ttooz_av","1.097"],
			 ["tonnetolb","2205"],
			 ["tonnetoston","1.10156"],
			 ["tonnetolton","0.9842"],
			 ["ltontotonne","1.016"],
			 ["ltontolb","2240"],
			 ["ltontoston","1.12"],
			 ["stontolb","2000"],
			 ["stontolton","0.89287"],
			 ["stontotonne","0.9078"],

			 // Time
 			 ["daytominute","1440"],
 			 ["daytosecond","86400"],
 			 ["minutetoday","0.000694444444444444444444"],
 			 ["minutetosecond","60"],
 			 ["secondtoday","0.0000115740740740740"],
 			 ["secondtominute","0.016666666666666667"],

			 // Pressure
			 ["atmtocmhg","76"],
			 ["atmtopsi","14.7"],
			 ["atmtoftwater","33.9"],
			 ["atmtoinchhg","29.92"],
			 ["cmhgtoatm","0.01316"],
			 ["cmhgtopsi","0.1934"],
			 ["cmhgtoftwater","0.44461"],
			 ["ftwatertoatm","0.0295"],
			 ["ftwatertoinchhg","0.8826"],
			 ["ftwatertopsi","0.4335"],
			 ["ftwatertokpa","2.9909"],
			 ["inchhgtoatm","0.03342"],
			 ["inchhgtoftwater","1.133"],
			 ["inchhgtopsi","0.4912"],
			 ["bartoatm","0.9869"],
			 ["bartopsi","14.5"],
			 ["bartokpa","100"],
			 ["psitoatm","0.06804"],
			 ["psitoftwater","2.307"],
			 ["psitoinchhg","2.036"],
			 ["kpatopsi","0.145"],
			 ["kpatobar","0.01"],
			 ["kpatoatm","0.009869"],
			 ["kpatoinchhg","0.2953"],
			 ["kpatoftwater","0.33455"],

			 // Volume
			 ["barreltousg","31.5"],
			 ["barreltooilg","42"],
			 ["barreltom3","0.1590"],
			 ["busheltocuft","1.2445"],
			 ["busheltom3","0.03524"],
			 ["busheltol","35.24"],
			 ["cufttom3","0.02832"],
			 ["cufttousg","7.48052"],
			 ["cufttol","28.32"],
			 ["m3tocuft","35.31"],
			 ["m3tousg","264.2"],
			 ["m3tobarrel","6.28"],
			 ["usgtocuft","0.1337"],
			 ["usgtom3","0.003785"],
			 ["usgtol","3.785"],
			 ["usgtoimpg","0.8326741"],
			 ["impgtousg","1.20095"],
			 ["impgtol","4.5456"],
			 ["ltocuft","0.03531"],
			 ["ltousg","0.2642"],

			 // Energy/Power
			 ["btutokj","1.0548"],
			 ["btutokwh","0.0002928"],
			 ["btuphtow","0.2931"],
			 ["hptobtupm","42.44"],
			 ["hptokw","0.7457"],
			 ["kjtobtu","0.948"],
			 ["kjtokwh","0.0002778"],
			 ["kwtohp","1.341"],
			 ["kwhtobtu","3413"],
			 ["kwhtokj","3600"],

			 // Velocity TODO
			 ["cuftpmtousgpm","7.482"],
			 ["cuftpmtolps","0.472"],

			 ["fpstomph","0.6818"],
			 ["fpstokmph","1.097"],

			 ["usgpmtocuftpm","0.002228"],
			 ["usgpmtolps","0.6308"],
			 ["usgpmtom3ph","0.22727"],

			 ["mpstomph","2.237"],
			 ["mpstokmph","3.6"],

			 ["mphtokmph","1.609"],
			 ["mphtoknots","0.8684"],
			 ["mphtomps","0.447"],

			 // Temperature
			 ["fahrenheittocelsius", "(5/9)*(var-32)"],
			 ["fahrenheittokelvin", "(5/9)*(var-32)+273"],
			 ["celsiustofahrenheit", "(1.8*var)+32"],
			 ["celsiustokelvin", "var+273"],
			 ["kelvintocelsius", "var-273"],
			 ["kelvintofahrenheit", "(1.8*(var-273))+32"]
			 );
    var i;
    for (i = 0; i < conv.length; i++) {
      if (func != conv[i][0])
	continue;
      expr = conv[i][1];
      if (expr.indexOf("var") == -1)
	expr += "*var";
      re = /var/gi;
      expr = expr.replace(re, num);
      var res = new Number(eval(expr));
      return res.toString();
    }
    return WRONG_FUNC;
  },

  base10To : function(base, number) {
    var res = new Number(number);
    return res.toString(parseInt(base))+"__conv__";
  },

  ToBase10 : function(base, number) {
    var res = parseInt(number, parseInt(base));
    return res.toString();
  }

//////////////////////////////////////////
// Other conversion definitions go here //
//////////////////////////////////////////

}
